package com.yanqu.road.dao.impl.activity.newyearbag;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.newyearbag.NewYearBagDevice;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class NewYearBagDeviceDaoImpl extends TempDataObjectDao<NewYearBagDevice> {

    public static String getNewYearBagDeviceSql = "select * from `t_c_new_year_bag_device` where `activityId`=?";

    public Map<String, NewYearBagDevice> getNewYearBagDeviceMap(int activityId) {
        Map<String, NewYearBagDevice> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getNewYearBagDeviceSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                NewYearBagDevice parse = parse(rs);
                map.put(parse.getDeviceId(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateNewYearBagDeviceSql = "update `t_c_new_year_bag_device` set xianJinTimes=?,shiWuTimes=? where activityId=? and deviceId=?";

    @Override
    protected boolean updateData(NewYearBagDevice newYearBagDevice) {
        newYearBagDevice.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, newYearBagDevice.getXianJinTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, newYearBagDevice.getShiWuTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, newYearBagDevice.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, newYearBagDevice.getDeviceId()));
        if (getDb().executeNoneQuery(updateNewYearBagDeviceSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertNewYearBagDeviceSql = "insert into `t_c_new_year_bag_device`(activityId,deviceId,xianJinTimes,shiWuTimes) values (?,?,?,?)";

    @Override
    protected boolean addData(NewYearBagDevice newYearBagDevice) {
        newYearBagDevice.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, newYearBagDevice.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, newYearBagDevice.getDeviceId()));
        para.put(i++, new DbParameter(Types.INTEGER, newYearBagDevice.getXianJinTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, newYearBagDevice.getShiWuTimes()));
        if (getDb().executeNoneQuery(insertNewYearBagDeviceSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected NewYearBagDevice parseData(ResultSet rs) throws SQLException {
        NewYearBagDevice newYearBagDevice = new NewYearBagDevice();
        newYearBagDevice.setActivityId(rs.getInt("activityId"));
        newYearBagDevice.setDeviceId(rs.getString("deviceId"));
        newYearBagDevice.setXianJinTimes(rs.getInt("xianJinTimes"));
        newYearBagDevice.setShiWuTimes(rs.getInt("shiWuTimes"));
        return newYearBagDevice;
    }

}
